package com.zynn.service.module.es.controller;

import com.zynn.common.core.base.BaseController;
import com.zynn.common.pojo.dto.EventDTO;
import com.zynn.service.module.es.service.EventsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Min;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @author admin
 *
 * es feignService
 */
@RestController
@RequestMapping("/event")
public class EventController extends BaseController {

    @Autowired
    private EventsService eventsService;

    /**
     * @param userId      用户id
     * @param groupIds    相册id
     * @param eventTypes  动态类型
     * @param lastEventId 最后一个动态id
     * @param pageSize    每页显示的数据条数,默认:30
     * @param isAsc       排序是否是升序:true-asc,false-desc,默认:true
     * @param useLimit    是否使用分页:true-使用,false-不使用,默认:false
     * @return
     */
    @GetMapping("/get/events")
    public List<EventDTO> getEvents(@RequestParam(required = false) Long userId,
                                    @RequestParam(required = false) String groupIds,
                                    @RequestParam(required = false) String eventTypes,
                                    @RequestParam(required = false) Long lastEventId,
                                    @RequestParam(required = false, defaultValue = "30") Long pageSize,
                                    @RequestParam(required = false, defaultValue = "true") Boolean isAsc,
                                    @RequestParam(required = false, defaultValue = "false") Boolean useLimit) {
        List<Long> groupIdList = new ArrayList<>();
        if (Objects.nonNull(groupIds)) {
            groupIdList.addAll(Arrays.stream(groupIds.split(",")).filter(StringUtils::isNotBlank).map(Long::valueOf).collect(Collectors.toList()));
        }
        List<Integer> eventTypeList = new ArrayList<>();
        if (Objects.nonNull(eventTypes)) {
            eventTypeList.addAll(Arrays.stream(eventTypes.split(",")).filter(StringUtils::isNotBlank).map(Integer::valueOf).collect(Collectors.toList()));
        }
        return eventsService.getEvents(userId, groupIdList, eventTypeList, lastEventId, pageSize, isAsc, useLimit);
    }


    /**
     * 查询用户当天的动态
     *
     * @param userId
     * @param createTime
     * @return
     */
    @GetMapping("/group/users")
    public List<EventDTO> getUserCurrentDayEvents(@RequestParam("userId") Long userId, @RequestParam("createTime") String createTime) {
        return eventsService.getUserCurrentDayEvents(userId, createTime);
    }

    /**
     * 查询某一个动态
     *
     * @param eventId
     * @return
     */
    @GetMapping("/get/event")
    public EventDTO getEvent(@RequestParam @Min(0) Long eventId) {
        return eventsService.getEvent(eventId);
    }


    /**
     * 查询用户相册内的动态
     *
     * @param userId
     * @param groupId
     * @param lastEventId
     * @param useLimit
     * @return
     */
    @GetMapping("/by/user")
    public List<EventDTO> queryEventByUser(@RequestParam Long userId,
                                           @RequestParam(required = false) Long groupId,
                                           @RequestParam(required = false) Long lastEventId,
                                           @RequestParam boolean useLimit) {
        return eventsService.queryEventByUser(userId, groupId, lastEventId, useLimit);
    }


    /**
     * 动态点赞排行（动态）
     *
     * @param groupId
     * @param limit
     * @return
     */
    @GetMapping("/like/sort")
    public List<EventDTO> getEventsByLikeSort(@RequestParam("groupId") Long groupId, @RequestParam("limit") Integer limit) {
        return eventsService.getEventsByLikeSort(groupId, limit);
    }

    /**
     * 照片数排行（用户）
     *
     * @param groupId
     * @param limit
     * @return
     */
    @GetMapping("/resource/sort")
    public List<Map<String, Object>> getEventsByResourceSort(@RequestParam("groupId") Long groupId, @RequestParam("limit") Integer limit) {
        return eventsService.getEventsByResourceSort(groupId, limit);
    }
}
